<template>
    <div class="genus-trend container-bottom-20">
        <CardBox title="物候观测趋势"></CardBox>
        <div class="content-container content-container-height-262">
            <div class="genus-trend-chart" ref="genusTrendChart" style="height: 262px;"></div>
        </div>
    </div>
</template>

<script>
import * as echarts from 'echarts'
import CardBox from '@/components/Global/CardBox.vue'
export default {
    components: {
        CardBox
    },
    data() {
        return {
            genusTrendChart: null,
            chartData: {
                dates: ['2024/1','2024/2','2024/3', '2024/4', '2024/5', '2024/6', '2024/7'],
                identify: [100, 120, 90, 100, 110],
                total: []
            }
        }
    },
    mounted() {
        this.getData()
    },
    methods: {
        getData(){
            this.$api.patrol.groupByMonth(new Date().getFullYear().toString()).then(res => {
                if(res&&res.length>0){
                    this.chartData.dates= res.map(item => (
                        item.year+"/"+item.month
                    ));
                    this.chartData.identify= res.map(item => (
                        item.count
                    ));
                    this.initGenusTrendChart();
                }

            });
        },
        initGenusTrendChart() {
            const chartDom = this.$refs.genusTrendChart
            if (!chartDom) return

            this.genusTrendChart = echarts.init(chartDom)

            const option = {
                tooltip: {
                    trigger: 'axis',
                    backgroundColor: 'rgba(0,24,106,0.8)',
                    borderColor: 'rgba(0,150,255,0.3)',
                    textStyle: {
                        color: '#fff',
                        fontFamily: 'DIN Pro'
                    }
                },
                legend: {
                    data: ['识别', '总量'],
                    textStyle: {
                        color: '#fff',
                        fontFamily: 'DIN Pro'
                    },
                    top: 10,
                    right: 10
                },
                grid: {
                    top: '15%',
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: this.chartData.dates,
                    axisLine: {
                        lineStyle: {
                            color: 'rgba(255,255,255,0.3)'
                        }
                    },
                    axisLabel: {
                        color: '#fff',
                        fontFamily: 'DIN Pro'
                    }
                },
                yAxis: {
                    type: 'value',
                    splitLine: {
                        lineStyle: {
                            color: 'rgba(255,255,255,0.1)'
                        }
                    },
                    axisLine: {
                        lineStyle: {
                            color: 'rgba(255,255,255,0.3)'
                        }
                    },
                    axisLabel: {
                        color: '#fff',
                        fontFamily: 'DIN Pro'
                    }
                },
                series: [
                    {
                        name: '数量',
                        type: 'line',
                        data: this.chartData.identify,
                        lineStyle: {
                            color: '#3DD78C'
                        },
                        itemStyle: {
                            color: '#3DD78C'
                        },
                        areaStyle: {
                            color: {
                                type: 'linear',
                                x: 0,
                                y: 0,
                                x2: 0,
                                y2: 1,
                                colorStops: [{
                                    offset: 0,
                                    color: 'rgba(61,215,140,0.3)'
                                }, {
                                    offset: 1,
                                    color: 'rgba(61,215,140,0)'
                                }]
                            }
                        },
                        smooth: true
                    },
                    // {
                    //     name: '总量',
                    //     type: 'line',
                    //     data: this.chartData.total,
                    //     lineStyle: {
                    //         color: '#3DD78C'
                    //     },
                    //     itemStyle: {
                    //         color: '#3DD78C'
                    //     },
                    //     areaStyle: {
                    //         color: {
                    //             type: 'linear',
                    //             x: 0,
                    //             y: 0,
                    //             x2: 0,
                    //             y2: 1,
                    //             colorStops: [{
                    //                 offset: 0,
                    //                 color: 'rgba(61,215,140,0.3)'
                    //             }, {
                    //                 offset: 1,
                    //                 color: 'rgba(61,215,140,0)'
                    //             }]
                    //         }
                    //     },
                    //     smooth: true
                    // }
                ]
            }

            this.genusTrendChart.setOption(option)
        }
    },
    beforeDestroy() {
        // 组件销毁前清理图表实例
        if (this.genusTrendChart) {
            this.genusTrendChart.dispose()
            this.genusTrendChart = null
        }
    }
}
</script>

<style lang="scss" scoped>
.genus-trend {
}
</style>
